package com.zhwmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhwmall.common.entity.product.AttrEntity;
import com.zhwmall.common.utils.PageUtils;
import com.zhwmall.common.utils.R;
import com.zhwmall.product.service.AttrGroupService;
import com.zhwmall.product.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 商品属性
 *
 * @author zhw
 * @email sqzhou@88.com
 * @date 2020-08-18 16:52:35
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrGroupService attrGroupService;

    @RequestMapping("/sale/list/{catId}")
    public R getSaleAttr(@RequestParam Map<String, Object> params, @PathVariable(value = "catId") long catId){
        PageUtils pageList = attrService.getSaleAttr(params, catId);
        return R.ok().put("data", pageList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable(value = "catId") long catId){

        IPage<AttrEntity> pageList = attrService.pageList(params, catId);
        return R.ok().put("data", pageList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);
        Long[] value = attrGroupService.findParent(attr.getCatelogId());
        attr.setValuePath(value);
        return R.ok().put("data", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrEntity attr){
		attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
//		attrService.removeByIds(Arrays.asList(attrIds));

        int attr = attrService.removeAttrByIds(Arrays.asList(attrIds));

        if (attr == 0){
            return R.error().put("data", "删除失败，此属性有关联");
        }
        return R.ok().put("data", "删除成功");
    }

}
